<?php

class Express extends Base_Controller
{


    public function __construct()
    {
        parent::__construct();
        defined('EBusinessID') or define('EBusinessID', 'test1333714');
//电商加密私钥，快递鸟提供，注意保管，不要泄漏
        defined('AppKey') or define('AppKey', 'abdb2540-ef66-4827-a261-cde42dd5cdd8');
//请求url
        defined('ReqURL') or define('ReqURL', 'http://140.143.56.78:7777/express/getOrderTracesByJson');
        $this->load->model('Express_model', 'express');
    }


    public function list()
    {
        $list = $this->express->getList();
        $data = ['express_list' => $list];
        $this->load->view('express/index', $data);
    }

    public function editview()
    {
        $express_id = $this->input->get('express_id', true) ?? 0;
        $recordInfo = $this->express->getDetail($express_id);
        $this->load->view('express/add', $recordInfo);
    }

    public function update()
    {
        try {
            $express_id = $this->input->post('express_id', true) ?? 0;

            $params['name']            = $this->input->post('name', true) ?? 3;
            $params['express_marking'] = $this->input->post('express_marking', true) ?? 3;
            $params['sort_order']      = $this->input->post('sort_order', true) ?? '';
            if ($express_id) {
                $params['create_time'] = date('Y-m-d H:i:s');
                $result                = $this->express->edit($express_id, $params);
                if (!$result) throw new RuntimeException("编辑失败");
            } else {
                $result = $this->express->add($params);
                if (!$result) throw new RuntimeException("添加失败");
            }
            if (!$result) {
                $this->error("添加失败");
            }
            $this->success();
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }

    }

    public function delete()
    {
        try {
            $express_id = $this->input->post('express_id', true) ?? 0;
            if (empty($express_id)) {
                throw new InvalidArgumentException("id不能为空");
            }
            $result = $this->express->delete($express_id);
            if (!$result) {
                throw new RuntimeException("删除失败");
            }
            $this->success();
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    public function searchExpress()
    {

        $order_number = $this->input->get('order_number', true) ?? 0;
        if(!$order_number){
            $this->error("订单号不能为空");
        }
        $this->load->model('Bill_model', 'bill');
        $record = $this->bill->getRecordByOrderNumber($order_number,' provider,express_number ',['type'=>2]);
        if(empty($record)){
            $this->error("信息不存在");
        }

        $expressInfo = $this->express->getRecordBy(['name'=>$record[0]['provider']]);
        if(!$expressInfo){
            $this->error("物流表数据异常");
        }
        $url           = "http://www.kuaidi100.com/query?type={$expressInfo['express_marking']}&postid={$record[0]['express_number']}&temp=".time();
        $result        = get_data($url);
        $expressDetail = json_decode($result, true);
        if ($expressDetail['status'] != 200) {
            $this->error("查询错误");
        }
        foreach ($expressDetail['data'] as $key => $row) {
            $expressDetail['data'][$key]['year'] = date("Y-m-d", strtotime($row['time']));
            $expressDetail['data'][$key]['time'] = date("H:i:m", strtotime($row['time']));
        }
        $this->success(['express_name'=>$expressInfo['name'],'express_data'=>$expressDetail['data']]);
    }

}
